package com.tqjc.plccontrol.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.plccontrol.entity.PlcInfo;
import com.tqjc.plccontrol.entity.PointInfo;
import com.tqjc.plccontrol.mapper.PlcInfoMapper;
import com.tqjc.plccontrol.mapper.PointInfoMapper;
import com.tqjc.plccontrol.service.PlcInfoService;
import com.tqjc.plccontrol.service.PointInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @projectName: BaotouSteel
 * @className: PlcInfoServiceImpl
 * @author: Patienyon
 * @description: PLC信息服务实现类
 * @date: 2023/6/13 10:19
 */
@Service
public class PlcInfoServiceImpl extends ServiceImpl<PlcInfoMapper, PlcInfo> implements PlcInfoService {
    @Resource
    private PlcInfoMapper plcInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public PlcInfo queryById(Integer id){
        return plcInfoMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param plcInfo 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PlcInfo> paginQuery(PlcInfo plcInfo, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PlcInfo> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(plcInfo.getName())){
            queryWrapper.eq(PlcInfo::getName, plcInfo.getName());
        }
        if(StrUtil.isNotBlank(plcInfo.getBrand())){
            queryWrapper.eq(PlcInfo::getBrand, plcInfo.getBrand());
        }
        if(StrUtil.isNotBlank(plcInfo.getModel())){
            queryWrapper.eq(PlcInfo::getModel, plcInfo.getModel());
        }
        if(StrUtil.isNotBlank(plcInfo.getIpAddress())){
            queryWrapper.eq(PlcInfo::getIpAddress, plcInfo.getIpAddress());
        }
        if(StrUtil.isNotBlank(plcInfo.getPort())){
            queryWrapper.eq(PlcInfo::getPort, plcInfo.getPort());
        }
        if(StrUtil.isNotBlank(plcInfo.getRemarks())){
            queryWrapper.eq(PlcInfo::getRemarks, plcInfo.getRemarks());
        }
        //2. 执行分页查询
        Page<PlcInfo> pagin = new Page<>(current , size , true);
        IPage<PlcInfo> selectResult = plcInfoMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param plcInfo 实例对象
     * @return 实例对象
     */
    public PlcInfo insert(PlcInfo plcInfo){
        plcInfoMapper.insert(plcInfo);
        return plcInfo;
    }

    /**
     * 更新数据
     *
     * @param plcInfo 实例对象
     * @return 实例对象
     */
    public PlcInfo update(PlcInfo plcInfo){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PlcInfo> chainWrapper = new LambdaUpdateChainWrapper<PlcInfo>(plcInfoMapper);
        if(StrUtil.isNotBlank(plcInfo.getName())){
            chainWrapper.eq(PlcInfo::getName, plcInfo.getName());
        }
        if(StrUtil.isNotBlank(plcInfo.getBrand())){
            chainWrapper.eq(PlcInfo::getBrand, plcInfo.getBrand());
        }
        if(StrUtil.isNotBlank(plcInfo.getModel())){
            chainWrapper.eq(PlcInfo::getModel, plcInfo.getModel());
        }
        if(StrUtil.isNotBlank(plcInfo.getIpAddress())){
            chainWrapper.eq(PlcInfo::getIpAddress, plcInfo.getIpAddress());
        }
        if(StrUtil.isNotBlank(plcInfo.getPort())){
            chainWrapper.eq(PlcInfo::getPort, plcInfo.getPort());
        }
        if(StrUtil.isNotBlank(plcInfo.getRemarks())){
            chainWrapper.eq(PlcInfo::getRemarks, plcInfo.getRemarks());
        }
        //2. 设置主键，并更新
        chainWrapper.set(PlcInfo::getId, plcInfo.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(plcInfo.getId());
        }else{
            return plcInfo;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = plcInfoMapper.deleteById(id);
        return total > 0;
    }
}
